// ************************************************************* //
//                                                               //
//        Copyright (C) 2015 by Socionext Inc.                   //
//        All rights reserved.                                   //
//        Socionext Inc. Proprietary and Confidential.           //
//                                                               //
// ************************************************************* //
/*****************************************************************************/
/**
 * @file em_types.h
 * @brief common type definition
 * @note None
 * @attention None
 *
 */
/*****************************************************************************/

#ifndef EM_TYPES_H
#define EM_TYPES_H

#include "em_config.h"
#include "em_host_register.h"
#include "em_dev_register.h"
#include "em_int_cmn.h"

/// host information structure
typedef struct EM_HOST EM_HOST ;

/**
 * @struct EM_REQUEST_FLAGS
 * @brief flag group for command requst information
 *
 */
typedef struct EM_REQUEST_FLAGS
{
	/// Command Type 
	EM_COMMAND_TYPE e_commandType;
	/// Response type
	EM_RESPONSE_TYPE e_responseType;
	/// data buffer access direction
	EM_TRANSFER_DIRECTION e_dataTransferDirection;
	/// @brief presence or absence flag for data buffer access
	/// @details 1 - data presence, 0 - data absence
	unsigned char uc_dataPresent;
	/// @brief presence or absense flag for DMA use(result detection)
	/// @details 1 - use, 0 - unuse
	unsigned char uc_dma_use;
	/// padding
	unsigned char uc_reserv[2];
}  EM_REQUEST_FLAGS;


/**
 * @struct EM_REQUEST_PARAM
 * @brief command request information
 *
 */

typedef struct EM_REQUEST_PARAM
{
	/// pointer of Response information
	unsigned long* p_response;
	/// pointer of read buffer when data access
	void* pDataBuffer;
	/// buffer pointer durring accessing
	void* pBufferPos;
	/// pointer of ADMA descriptor table
	unsigned long* pAdmaDescriptorTable;
	/// pointer of corresponding host structure
	EM_HOST* pOwnerHost;
	/// flag group of Request information
	EM_REQUEST_FLAGS sRequestFlags;
	/// Set Argument to SRS2  when command issue
	unsigned long ul_argument;
	/// destination for response
	unsigned long ul_responseTab[ EM_RESP_TAB_SIZE ];
	/// number of block
	unsigned long ul_blockCount;
	/// byte count for access
	unsigned long ul_dataRemaining;
	/// lenght of block
	unsigned short us_blockLen;
	/// command number
	unsigned char uc_command;
	/// state of command request
	unsigned char uc_status;
	/// recorded area for response flag
	unsigned char uc_InterruptCheck ;
	/// size of Response
	unsigned char uc_respSize;
	/// padding
	unsigned char uc_reserv[2];
}  EM_REQUEST_PARAM;

/**
 * @struct EM_MEMORY_CARD_INFO
 * @brief card information structure
 */
typedef struct EM_MEMORY_CARD_INFO
{
	/// device size
	unsigned long ul_DeviceSizeMB;
	/// maximum trasfer speed
	unsigned long ul_max_frequency;
	/// block size
	unsigned short us_BlockSize;
	/// padding
	unsigned char uc_reserv[2];
} EM_MEMORY_CARD_INFO, *PEM_MEMORY_CARD_INFO;
	
/**
 * @struct EM_DEVICE
 * @brief devixe information structure
 */
typedef struct EM_DEVICE
{
	/// pointer of corresoponding slot structure
	struct EM_SLOT *pSlot;
	/// card information
	EM_MEMORY_CARD_INFO sCardInfo;
	/// Relative card address
	unsigned short us_RCA;
	/// device capatity decieded by OCR Register/AccessMode
	unsigned char uc_DeviceCapacity;
	/// Specification Version Number
	unsigned char uc_SpecVersNumb;
	/// supported bus width @ref EM_BUS_WIDTH reference
	unsigned char uc_SupportedBusWidths;
	/// applicable device selecting state, which is changed by CMD7
	unsigned char uc_IsSelected;
	/// padding
	unsigned char uc_reserv[2];
} EM_DEVICE;

/**
 * @struct EM_SLOT
 * @brief slot information structure
 *
 */
typedef struct EM_SLOT {
	/// work area
	unsigned long ul_AuxBuff[ EXTCSD_SIZE / 4 ];
	/// Extends CSD strage area
	unsigned long ul_ExtendsCSD[ EXTCSD_SIZE / 4 ];
	/// CSD register information
	unsigned long ul_CSDinfo[4];
	/// base address of slot register(SRS)
	EM_REGISTER_SRS *pRegSRS ; 
	/// poointer of selected device structure
	EM_DEVICE *pDevice;
	/// pointer of command requst structure which is worked on applicable slot
	EM_REQUEST_PARAM* pCurrentRequest;
	/// pointer of parent host structure
	struct EM_HOST *pParentHost;
	/// pointer of descriptoer buffer
	unsigned long *pDescriptorBuffer;
	/// access mode
	EM_ACCESS_MODE e_AccessMode;
	/// DMA mode
	EM_DMAMODE eDmaMode;
	/// DMA boundary, is set to SRS1
	unsigned long ul_DMABufferBoundary;
	/// device information structure
	EM_DEVICE sDevices[ EM_CONFIG_MAX_DEV_PER_SLOT ];
	/// bus width
	unsigned char uc_BusWidth;
	/// @brief card inserted state
	/// @details 1 - inserted, 0 - no inserted(unconfirmed)
	unsigned char uc_CardInserted;
	/// number of slot
	unsigned char uc_SlotNum;
	/// @brief presence or absence attach request
	/// @details flag detected attatched processing is finished after card was inserted
	unsigned char uc_NeedAttach;
	/// in error recovery
	unsigned char uc_errorRecover ;
	/// padding
	unsigned char uc_reserv[3];

} EM_SLOT;

/**
 * @struct EM_HOST
 * @brief host information structure
 *
 */
struct EM_HOST {
	/// slot information structure
	EM_SLOT sSlots[ EM_CONFIG_SLOT_COUNT ];
	/// number of valid slot
	unsigned char uc_SlotCount;
	/// Specification Version Number
	unsigned char uc_SpecVerNum;
	/// base address of host register(HRS)
	EM_REGISTER_SFR *pBaseAddress ;
	/// padding
	unsigned char uc_reserv[2];
} ;

#endif
